using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using Java.db;
using System.IO; 
using System.Reflection;


public partial class UtilWeb_UC_UCRepTituloSin : System.Web.UI.UserControl
{
    public IExcel iexcel;
    public String[] campos;
    public String[] cabeceras;
    public String mostraExcel;
    
    #region VARIABLES

    protected String isMensaje;
    #endregion

    #region PROPIEDADES
    private String _Descipcion;
  //  private Int32 _NumRegistro;
    private Dictionary<String, String> _DicUrl;
    private String _cod_aeliminar;
    private Boolean _Restaurar;
    

    public Boolean Restaurar
    {
        get { return _Restaurar; }
        set { _Restaurar = value; }
    }
    public String Cod_aeliminar
    {
        get { return _cod_aeliminar; }
        set { _cod_aeliminar = value; }
    }

    public String Descripcion
    {
        get { return _Descipcion; }
        set { _Descipcion = value; }
    }

    public Int32 NumRegistro
    {
        get { return 0; }
        set {  }
    }
    public Dictionary<String, String> DicUrl
    {
        get { return _DicUrl; }
        set { _DicUrl = value; }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        subPonerValores();
    }


    #region excel


    protected void lnkExportar_Click(object sender, EventArgs e)
    {
        
       List<AbstractBean> lista = iexcel.getListaExcel();

       if (lista.Capacity > 0)
       {
            ConfigurarGridview(gvExcel, lista);

            gvExcel.DataBind();
            gvExcel.Visible = false;
            subExportar();
        }
        
    }

    private void ConfigurarGridview(GridView gvExcel, List<AbstractBean> lista)
    {
       // object oBE = lista[0];
       // Type oType = oBE.GetType();
      // PropertyInfo[] pi = oType.GetProperties();
        gvExcel.AutoGenerateColumns = false;
        int i = 0;


            foreach (String p in campos)
            {
                BoundField bc = new BoundField();
                bc.DataField = p;
                if (i < cabeceras.Length)
                {
                    bc.HeaderText = cabeceras[i];
                }
                gvExcel.Columns.Add(bc);
                i++;
            }
            gvExcel.DataSource = lista;

    }
    

   
    private void subExportar()
    {
        gvExcel.Visible = true;
        string style = @"<style> .text { mso-number-format:\@; } </script> ";
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
        Response.ContentType = "application/excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gvExcel.RenderControl(htw); //donde gvUsers es el nombre del gridview
        // Style is added dynamically
        Response.Write(style);

        Response.Write(sw.ToString());
        Response.End();
    }

   

   
    #endregion

  
    private String fnUrlNavegacion()
    {
        StringBuilder loSb = new StringBuilder();
        if (DicUrl != null)
        {
            foreach (KeyValuePair<string, string> lokvp in DicUrl)
            {
                loSb.Append("<a href=" + Request.ApplicationPath + lokvp.Value + " title='" + lokvp.Key + "'>" + lokvp.Key + "</a> >> ");

            }
            return loSb.ToString().Substring(0, loSb.Length - 3);
        }
        return "";
    }

    public void subPonerValores()
    {
        litDescripcion.Text = Descripcion;
        // litNumero.Text = NumRegistro.ToString();
        litUrlNavegacion.Text = fnUrlNavegacion();
    }

    protected void gvExcel_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                e.Row.Cells[i].Attributes.Add("class", "text");
            }
        }
    }
}
